<?php
require_once '../conn.php';
require_once '../libs/global.cls';

$mobile = $_POST['txtmobile'];
$email = $_POST['txtemail'];
$name = $_POST['txtname'];
$pwd1 = $_POST['txtpwd'];
$pwd2 = $_POST['txtrepwd'];

// 手机号为空
if(empty ($mobile)){
    $error_type = 119;
    include_once '../error/register_error.php';
    exit;
}

// 手机号码检验
if(!preg_match("/^13[0-9]{1}[0-9]{8}$|15[0189]{1}[0-9]{8}$|189[0-9]{8}$/",$mobile)){
    $error_type = 121;
    include_once '../error/register_error.php';
    exit;
}

// 邮箱地址为空
if(empty ($email)){
    $error_type = 101;
    include_once '../error/register_error.php';
    exit;
}

// 邮箱地址检验
$regemail = "^([a-za-z0-9_-])+@([a-za-z0-9_-])+(\.[a-za-z0-9_-])";
if(!ereg($regemail, $email)){
    $error_type = 103;
    include_once '../error/register_error.php';
    exit;
}

// 用户名为空
if(empty ($name)){
    $error_type = 105;
    include_once '../error/register_error.php';
    exit;
}

// 密码为空
if(empty ($pwd1)||empty ($pwd2)){
    $error_type = 107;
    include_once '../error/register_error.php';
    exit;
}

// 重新输入密码错误
if($pwd1 != $pwd2){
    $error_type = 109;
    include_once '../error/register_error.php';
    exit;
}

// 判断数据库姓名或email或手机号是否存在
$sql = "select name,emai，cellphone from cc_user where name='". $name ."' or emai='". $email ."' or cellphone='". $mobile ."'";
$res = $ms_cc_pdo->select($sql);
$row = $res['result'];
foreach ($row as $key=>$item){
    if($item['emai'] == $email){
        $error_type = 115;
        include_once '../error/register_error.php';
        exit;
    }
    if($item['name'] == $name){
        $error_type = 117;
        include_once '../error/register_error.php';
        exit;
    }
    if($item['cellphone'] == $mobile){
        $error_type = 123;
        include_once '../error/register_error.php';
        exit;
    }
}

// 验证通过，注册
// cc号生成规则：随机生成6-12位数字
$cc_id = "";
for( $i = 0; $i < 6; $i++){
    $cc_id .= rand(0,9);
}
// ip
$user_IP = ($_SERVER["HTTP_VIA"]) ? $_SERVER["HTTP_X_FORWARDED_FOR"] : $_SERVER["REMOTE_ADDR"];
$user_IP = ($user_IP) ? $user_IP : $_SERVER["REMOTE_ADDR"];

$params = array();
$params[':cc_id'] = (int)$cc_id;
$params[':name'] = $name;
$params[':password'] = md5($pwd1);
$params[':emai'] = $email;
$params[':ctime'] =  time();
$params[':cellphone'] = $mobile ;

//date('Y-m-d H:i:s',time());

//添加到数据库
$sql_insert_user = sprintf( "INSERT INTO cc_user (cc_id, name, password, emai, ctime, cellphone) VALUES(  :cc_id, :name, :password, :emai, :ctime, :cellphone )" );
//$sql_insert_user = sprintf( "INSERT INTO cc_user(`cc_id`, `name`, `password`) values (:cc_id, :name, :password)");

$res_insert = $ms_cc_pdo->insert($sql_insert_user,$params);
if(!$res_insert['errorcode']){
    redirect("login.php");
}

?>
